package com.xsq.page;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.xsq.R;
import com.xsq.view.DelayDialog;

public class BaseFragment extends Fragment {

    protected interface PriceType {
        int Input = 1;
        int Auto = 2;
    }

    private DelayDialog dialog;
    private static int MSG_DISMISS_DIALOG = 0;
    private static int MSG_DISMISS_DIALOG_TODO = 1;
    private static int MSG_DISMISS_DIALOG_WITH_BUTTON = 2;
    private DialogCloseListener dialogCloseListener;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(MSG_DISMISS_DIALOG == msg.what){
                if(null != dialog){
                    if(dialog.isShowing()){
                        dialog.dismiss();
                    }
                }
            }else if(MSG_DISMISS_DIALOG_TODO == msg.what) {
                if(null != dialog){
                    if(dialog.isShowing()){
                        dialog.dismiss();
                    }
                    if(dialogCloseListener != null) {
                        dialogCloseListener.onClose();
                    }
                }
            }else if(MSG_DISMISS_DIALOG_WITH_BUTTON == msg.what) {
                if(null != dialog){
                    if(dialog.isShowing()){
                        dialog.dismiss();
                    }
                    dialog = null;
                }
            }
        }
    };

    /*** 不带按钮的Dialog，关闭无回调的 ***/
    protected void showDialog(String loadingText, long delayCloseMillis) {
        if(dialog == null) {
            dialog = new DelayDialog(getActivity(), R.style.LoadingDialog);
        }
        dialog.show();
        dialog.setMessageText(loadingText);
        dialog.getCancelBtn().setVisibility(View.GONE);
        mHandler.sendEmptyMessageDelayed(MSG_DISMISS_DIALOG, delayCloseMillis);
    }

    /*** 不带按钮的Dialog，关闭有回调的 ***/
    protected void showDialog(String loadingText, long delayCloseMillis, DialogCloseListener dialogCloseListener) {
        if(dialog == null) {
            dialog = new DelayDialog(getActivity(), R.style.LoadingDialog);
        }
        dialog.show();
        dialog.setMessageText(loadingText);
        dialog.getCancelBtn().setVisibility(View.GONE);
        this.dialogCloseListener = dialogCloseListener;
        mHandler.sendEmptyMessageDelayed(MSG_DISMISS_DIALOG_TODO, delayCloseMillis);
    }

    /*** 带按钮的Dialog ***/
    protected void showDialog(String loadingText, EditText editCard, String btnText, final DialogClickCancelListener onClickCancelListener) {
        if(dialog == null) {
            dialog = new DelayDialog(getActivity(), R.style.LoadingDialog);
        }
        dialog.show();
        dialog.setMessageText(loadingText);
        dialog.getEditText().setText("");
        dialog.notify(editCard);
        dialog.getCancelBtn().setText(btnText);
        dialog.getCancelBtn().setVisibility(View.VISIBLE);
        dialog.getCancelBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickCancelListener.onClickCancel();
            }
        });
    }
    /*** 带按钮的Dialog关闭 ***/
    protected void closeDialog() {
        mHandler.sendEmptyMessage(MSG_DISMISS_DIALOG_WITH_BUTTON);
    }

    interface DialogCloseListener {
        void onClose();
    }

    interface DialogClickCancelListener {
        void onClickCancel();
    }
}
